package day17;

public class Demo4 {
    static int sum = 100;//总数
    public static synchronized void buy(){
        String name = Thread.currentThread().getName();
        sum--;
        System.out.println(name+"成功购买了票,剩余:"+sum);
    }
    public static void main(String[] args) throws InterruptedException {
        Demo4 demo4 = new Demo4();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                demo4.buy();
            }
        },"窗口1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                demo4.buy();
            }
        },"窗口2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("剩余:"+demo4.sum);
    }
}
